#include <QApplication>
#include <QPushButton>
#include <QFont>
#include <QWidget>
#include <QSizePolicy>
#include <QLCDNumber>
#include <QSlider>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QObject>
#include <QListWidget>
#include <QString>
#include <QGridLayout>
#include <QColor>
#include <QCheckBox>
#include <QFileDialog>
#include <QLabel>
#include <QMessageBox>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include <qwt_legend.h>
#include <qwt_data.h>
#include <qwt_text.h>

#include <math.h>
#include <iostream>
#include <vector>
#include <sstream>

//#include "qplot.h"
#include "see.h"
#include "wdet.h"
#include "refwin.h"
#include "qmanager.h"
#include "globalconstants.h"

#include "errorwindow.h"
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    mainWindow xx;
    xx.setGeometry(10,10,800,600);
    xx.show();
    
    //Se cfrean y muestran las ventanitas de detector y de info
    wDet *myDet=new wDet();
    myDet->resize(400,400);
    
    qInfo *tankInfo=new qInfo();
    tankInfo->setGeometry(650,100,100,100);
    
    qRefWin *refWin=new qRefWin();
    
    myDet->show();
    tankInfo->show();
    
    //Ventana de dialogo que pregunta si se quiere entrar al programa
    QMessageBox msgBox(QString("calvin 0.05"),
                       QString("Pulse aceptar para configurara el detector e iniciar (puede tomar un par de minutos) o cancelar para salir"),
                       QMessageBox::NoIcon,
                       QMessageBox::Ok | QMessageBox::Ok,
                       QMessageBox::Cancel | QMessageBox::Cancel,
                       QMessageBox::NoButton);
    if(msgBox.exec()==QMessageBox::Cancel){
      cout << "\nEjecucion cancelada por el usuario\n\n";
      return 0;
    }
    
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    
    //see es la interfaz entre los eventos contenidos en los archivos y los objetos que componen calvin
    see *pepe=new see();
    
    //El qManager se encarga de controlar la mayoria de las conecciones entre los widgets
    qManager *master;
    master = new qManager(pepe);
    
    QApplication::restoreOverrideCursor();
    
    //Conexiones..
    QObject::connect(&xx,    SIGNAL(salir()),                               qApp,   SLOT(quit()));
    QObject::connect(&xx,    SIGNAL(abrir()),                               master, SLOT(openFile()));
    QObject::connect(&xx,    SIGNAL(externalTankSelect(int,eLT,int)),       master, SLOT(listTankChanged(int,eLT,int)));
    QObject::connect(&xx,    SIGNAL(eventSelected(int)),                    master, SLOT(changeEvent(int)));
    
    QObject::connect(master, SIGNAL(setEventsList(vector<tEventsummary>*)), &xx,    SLOT(setEventsList(vector<tEventsummary>*)));
    QObject::connect(master, SIGNAL(setTankList(vector<tCandidate>*)),      &xx,    SLOT(setTankList(vector<tCandidate>*)));
    QObject::connect(master, SIGNAL(listTankSelect(int)),                   &xx,    SLOT(tankSelect(int)));
    QObject::connect(master, SIGNAL(valueChanged(pmtTrace*)),               &xx,    SLOT(setValue(pmtTrace*)));
    
    QObject::connect(master, SIGNAL(newDetector(vector<tStation>*)),        myDet, SLOT(newDetector(vector<tStation>*)));
    QObject::connect(master, SIGNAL(detTankSelect(int,eLT)),                myDet, SLOT(tankSelect(int,eLT)));
    QObject::connect(master, SIGNAL(setInfo(tCandidate*)),                  tankInfo, SLOT(setInfo(tCandidate*)));
    
    QObject::connect(myDet,       SIGNAL(changedTank(int)),                 master,SLOT(detTankChanged(int)));
    
    //Los tres grupos siguientes vinculan los botones "detector", "referencias" e "info" con la visibilidad
    //de las ventanas
    QObject::connect(&xx,    SIGNAL(showDet(bool)),                         myDet,    SLOT(setVisible(bool)));
    QObject::connect(myDet,  SIGNAL(openWindow(bool)),                      &xx,      SLOT(showDetButton(bool)));
    
    QObject::connect(&xx,    SIGNAL(showRef(bool)),                         refWin,   SLOT(setVisible(bool)));
    QObject::connect(refWin,  SIGNAL(openWindow(bool)),                      &xx,     SLOT(showRefButton(bool)));
    
    QObject::connect(&xx,    SIGNAL(showInf(bool)),                         tankInfo, SLOT(setVisible(bool)));
    QObject::connect(tankInfo,  SIGNAL(openWindow(bool)),                   &xx,      SLOT(showInfButton(bool)));
    
    return app.exec();
}


